﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;
using System.IO;
using System.Data;

namespace School.Pages.Teaching
{
    public partial class TE_Topic : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1507) == true)
                {

                    if (!this.Page.IsPostBack)
                        FillControls();
                    lbltst.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");

        }
        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }
        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }

        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }

        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
            }
        }

        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedIndex > 0)
            {
                ddlBind(ddlsubject, School.Teaching.Teaching.Get_Subject_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue)));

                //Get_Class_By_Grad
                LbClasses.DataSource = School.Teaching.Teaching.Get_Class_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue));
                LbClasses.Items.Clear();
                LbClasses.Items.Insert(0, new ListItem("اختيار", "0"));

                LbClasses.DataBind();
                LbClasses.SelectedValue = "0";
            }
        }

        protected void ddlsubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlsubject.SelectedValue != "0")
            {
                ddlBind(ddlCourse, School.Teaching.Teaching.Get_Course_By_Subject(Convert.ToInt32(ddlsubject.SelectedValue)));
            }
        }

        protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCourse.SelectedIndex > 0)
            {
                gvTopicBind();


                //var query = School.Teaching.Teaching.Get_Topic_ByCourse(Convert.ToInt32(ddlCourse.SelectedValue));


            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            long re = School.Teaching.Teaching.Add_Topic(Convert.ToInt32(ddlCourse.SelectedValue.ToString()), txtName.Text, EDContent.Content.ToString());
            if (re != 0)
            {

                foreach (ListItem item in LbClasses.Items)
                {
                    if (item.Selected && item.Value != "0")
                    {
                        School.Teaching.Teaching.Add_Topic_Classes(Convert.ToInt32(re), Convert.ToInt32(item.Value.ToString()));
                    }
                }
                if (FileUpload1.HasFile)
                {
                    try
                    {
                        string filename = Path.GetFileName(FileUpload1.FileName);
                        FileUpload1.SaveAs(Server.MapPath("~/Attach/") + filename);
                        School.Teaching.Teaching.Add_Topic_Attach(Convert.ToInt32(re), filename);
                        //LabelStore.Text = filename;
                        //Imagelogo.ImageUrl = Request.ApplicationPath + "/Image/" + filename;
                    }
                    catch (Exception ex)
                    {
                        lbltst.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    }
                }


                if (FileUpload2.HasFile)
                {
                    try
                    {
                        string filename = Path.GetFileName(FileUpload2.FileName);
                        FileUpload1.SaveAs(Server.MapPath("~/Attach/") + filename);
                        School.Teaching.Teaching.Add_Topic_Attach(Convert.ToInt32(re), filename);
                        //LabelStore.Text = filename;
                        //Imagelogo.ImageUrl = Request.ApplicationPath + "/Image/" + filename;
                    }
                    catch (Exception ex)
                    {
                        lbltst.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    }
                }

                if (FileUpload3.HasFile)
                {
                    try
                    {
                        string filename = Path.GetFileName(FileUpload3.FileName);
                        FileUpload1.SaveAs(Server.MapPath("~/Attach/") + filename);
                        School.Teaching.Teaching.Add_Topic_Attach(Convert.ToInt32(re), filename);
                        //LabelStore.Text = filename;
                        //Imagelogo.ImageUrl = Request.ApplicationPath + "/Image/" + filename;
                    }
                    catch (Exception ex)
                    {
                        lbltst.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    }
                }

                if (FileUpload4.HasFile)
                {
                    try
                    {
                        string filename = Path.GetFileName(FileUpload4.FileName);
                        FileUpload1.SaveAs(Server.MapPath("~/Attach/") + filename);
                        School.Teaching.Teaching.Add_Topic_Attach(Convert.ToInt32(re), filename);
                        //LabelStore.Text = filename;
                        //Imagelogo.ImageUrl = Request.ApplicationPath + "/Image/" + filename;
                    }
                    catch (Exception ex)
                    {
                        lbltst.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    }
                }


                if (FileUpload5.HasFile)
                {
                    try
                    {
                        string filename = Path.GetFileName(FileUpload5.FileName);
                        FileUpload1.SaveAs(Server.MapPath("~/Attach/") + filename);
                        School.Teaching.Teaching.Add_Topic_Attach(Convert.ToInt32(re), filename);
                        //LabelStore.Text = filename;
                        //Imagelogo.ImageUrl = Request.ApplicationPath + "/Image/" + filename;
                    }
                    catch (Exception ex)
                    {
                        lbltst.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                    }
                }

                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تمت الاضافة بنجاح";
                txtName.Text = "";
                EDContent.Content = "";
                gvTopicBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvTopic_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvTopic.DataKeys[e.RowIndex].Value.ToString());
            bool re = true;

            var query = School.Teaching.Teaching.Get_Topic_Attach(Id).ToList();
            foreach (var item in query)
            {
                re = School.Teaching.Teaching.DeleteTopic_Attach(Convert.ToInt32(item.Id));
            }

            if (re == true)
            {

                re = School.Teaching.Teaching.DeleteTopic(Id);
                if (re == true)
                {
                    lbltst.ForeColor = Color.Green;
                    lbltst.Text = "تم الحذف بنجاح";
                    gvTopicBind();
                }
                else
                {
                    lbltst.ForeColor = Color.Red;
                    lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
                }
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        private void gvTopicBind()
        {
            gvTopic.DataSource = School.Teaching.Teaching.Get_Topic_ByCourse(Convert.ToInt32(ddlCourse.SelectedValue));
            gvTopic.DataBind();
        }

        protected void gvTopic_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }
    }
}